//
//  UIWidget.m
//  storeapp
//
//  Created by huangyi on 13-3-13.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "UIWidget.h"
#import "NSString+utils.h"
#import "UIFont+utils.h"
#import "UIColor+utils.h"
#import "UIView+utils.h"
#import "UIImageView+utils.h"

@implementation UIWidget

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+(UIWidget*)widgetWithTemplateFile:(NSString*)file{
    NSString *string=[NSString stringWithContentsOfFile:file usedEncoding:nil error:nil];
    return [UIWidget widgetWithTemplateString:string];
}

+(UIWidget*)widgetWithTemplateString:(NSString*)string{
    NSDictionary *dictionary=string.jsonObject;
    return [UIWidget widgetWithTemplateDictionary:dictionary];
}

+(UIWidget*)widgetWithTemplateDictionary:(NSDictionary*)dictionary{
    if (dictionary) {
        UIWidget *widget=[[UIWidget alloc] initWithFrame:CGRectZero];
        widget.templateDictionary=dictionary;
        [widget loadWidget];
        return widget;
    }else{
        return nil;
    }
}

-(void)loadWidget{
    NSDictionary *json=self.templateDictionary;
    self.identifier=json[@"id"];
    NSArray *views=json[@"views"];
    for (int i=0; i<views.count; i++) {
        NSDictionary *item=views[i];
        NSString *type=item[@"type"];
        if ([type isEqualToString:@"button"]) {
            UIView *view=[self buttonView:item];
            view.tag=i;
            [self addSubview:view];
        }
        if ([type isEqualToString:@"image"]) {
            UIView *view=[self imageView:item];
            view.tag=i;
            [self addSubview:view];
        }
        if ([type isEqualToString:@"text"]) {
            UIView *view=[self textView:item];
            view.tag=i;
            [self addSubview:view];
        }
        if ([type isEqualToString:@"textfield"]) {
            UIView *view=[self textfieldView:item];
            view.tag=i;
            [self addSubview:view];
        }
        if ([type isEqualToString:@"widget"]) {
            UIView *view=[UIWidget widgetWithTemplateDictionary:item];
            view.tag=i;
            [self addSubview:view];
        }
    }
    [self sizeToAuto];
}

-(void)setVar:(NSDictionary*)var{
    self.userInfo=var;
    NSDictionary *json=self.templateDictionary;
    NSArray *views=json[@"views"];
    for (int i=0; i<views.count; i++) {
        NSDictionary *item=views[i];
        NSString *identifier=item[@"id"];
        if (var[identifier]) {
            NSString *type=item[@"type"];
            if ([type isEqualToString:@"button"]) {
            }
            if ([type isEqualToString:@"image"]) {
                UIImageView *view=(id)[self subviewWithTag:i];
                view.image=nil;
                NSString *value=var[identifier];
                [view imageFromURL:value];
            }
            if ([type isEqualToString:@"text"]) {
                UILabel *view=(id)[self subviewWithTag:i];
                NSString *value=var[identifier];
                view.text=value;
            }
            if ([type isEqualToString:@"textfield"]) {
                UITextField *view=(id)[self subviewWithTag:i];
                NSString *value=var[identifier];
                view.placeholder=value;
            }
            if ([type isEqualToString:@"widget"]) {
                UIWidget *view=(id)[self subviewWithTag:i];
                NSDictionary *value=var[identifier];
                [view setVar:value];
            }
        }
    }
}

-(id)viewWithIdentifier:(NSString*)identifier{
    UIView *view=nil;
    NSDictionary *json=self.templateDictionary;
    NSArray *views=json[@"views"];
    for (int i=0; i<views.count; i++) {
        NSDictionary *item=views[i];
        NSString *str=item[@"id"];
        if ([str isEqualToString:identifier]) {
            view=[self subviewWithTag:i];
        }
    }
    return view;
}

-(UIView*)buttonView:(NSDictionary*)item{
    UIView *view=[[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor=[UIColor clearColor];
    view.userInteractionEnabled=YES;
    NSString *frame=item[@"frame"];
    if (frame) {
        view.frame=CGRectFromString(frame);
    }else{
        [view sizeToFit];
    }
    return view;
}
-(UIImageView*)imageView:(NSDictionary*)item{
    UIImageView *view=[[UIImageView alloc] initWithFrame:CGRectZero];
    view.backgroundColor=[UIColor clearColor];
    view.userInteractionEnabled=NO;
    NSString *frame=item[@"frame"];
    if (frame) {
        view.frame=CGRectFromString(frame);
    }else{
        [view sizeToFit];
    }
    NSString *image=item[@"image"];
    if (image) {
        view.image=nil;
        [view imageFromURL:image];
    }
    return view;
}
-(UILabel*)textView:(NSDictionary*)item{
    UILabel *view=[[UILabel alloc] initWithFrame:CGRectZero];
    view.backgroundColor=[UIColor clearColor];
    view.userInteractionEnabled=NO;
    view.numberOfLines=0;
    NSString *frame=item[@"frame"];
    if (frame) {
        view.frame=CGRectFromString(frame);
    }else{
        [view sizeToFit];
    }
    NSString *text=item[@"text"];
    view.text=text;
    NSString *color=item[@"color"];
    if (color) {
        view.textColor=UIColorFromString(color);
    }
    NSString *font=item[@"font"];
    if (font) {
        view.font=UIFontFromString(font);
    }
    NSString *align=item[@"align"];
    if ([align isEqualToString:@"left"]) {
        view.textAlignment=NSTextAlignmentLeft;
    }
    if ([align isEqualToString:@"center"]) {
        view.textAlignment=NSTextAlignmentCenter;
    }
    if ([align isEqualToString:@"right"]) {
        view.textAlignment=NSTextAlignmentRight;
    }
    return view;
}
-(UITextField*)textfieldView:(NSDictionary*)item{
    UITextField *view=[[UITextField alloc] initWithFrame:CGRectZero];
    view.backgroundColor=[UIColor clearColor];
    view.userInteractionEnabled=NO;
    NSString *frame=item[@"frame"];
    if (frame) {
        view.frame=CGRectFromString(frame);
    }else{
        [view sizeToFit];
    }
    NSString *placehold=item[@"placehold"];
    view.placeholder=placehold;
    NSString *color=item[@"color"];
    if (color) {
        view.textColor=UIColorFromString(color);
    }
    NSString *font=item[@"font"];
    if (font) {
        view.font=UIFontFromString(font);
    }
    NSString *align=item[@"align"];
    if ([align isEqualToString:@"left"]) {
        view.textAlignment=NSTextAlignmentLeft;
    }
    if ([align isEqualToString:@"center"]) {
        view.textAlignment=NSTextAlignmentCenter;
    }
    if ([align isEqualToString:@"right"]) {
        view.textAlignment=NSTextAlignmentRight;
    }
    return view;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=touches.anyObject;
    CGPoint pt=[touch locationInView:self];
    if (CGRectContainsPoint(self.bounds, pt)) {
        UIView *view=touch.view;
        NSDictionary *json=self.templateDictionary;
        NSArray *views=json[@"views"];
        NSDictionary *item=views[view.tag];
        NSString *execute=item[@"execute"];
        if (execute) {
            SEL selector=NSSelectorFromString(execute);
            if ([self.delegate respondsToSelector:selector]) {
                [self.delegate performSelector:selector withObject:self afterDelay:0];
            }
        }
    }
}

@end
